package com.example.l10_yanweicheng;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.l10_yanweicheng.recognition.BodyAttr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    String fileName, imageURL;
    File filePath;
    RadioButton radioButton, radioButton2, radioButton3, radioButton4;
    ImageView imageView;
    TextView textView;
    Boolean internalStorage, persistentFiles;

    private boolean isExternalStorageWriteable(){
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    private void downloadImage(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File writeFile = new File(filePath,fileName);
                    if(!writeFile.exists()){
                        URL url = new URL(imageURL);
                        URLConnection connection = (URLConnection) url.openConnection();
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        FileOutputStream outputStream = new FileOutputStream(writeFile);
                        bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                        outputStream.close();
                        inputStream.close();
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void readImage(){
        try {
            File readFile = new File(filePath,fileName);
            if(readFile.exists()){
                FileInputStream inputStream = new FileInputStream(readFile);
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                imageView.setImageBitmap(bitmap);
            } else {
                Toast.makeText(MainActivity.this,"This image doesn's exist!",Toast.LENGTH_LONG).show();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioButton = findViewById(R.id.radioButton);
        radioButton2 = findViewById(R.id.radioButton2);
        radioButton3 = findViewById(R.id.radioButton3);
        radioButton4 = findViewById(R.id.radioButton4);
        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView3);
        Button button =findViewById(R.id.button);
        Button button2 = findViewById(R.id.button2);

        if (!isExternalStorageWriteable()){
            radioButton2.setEnabled(false);
            Toast.makeText(MainActivity.this,"External Storage is not availavle!",Toast.LENGTH_LONG).show();
        }
        button2.setOnClickListener(v -> {
            specifyFilePathAndName();
            if(fileName!=null && filePath!=null){
                textView.setText(BodyAttr.body_attr(filePath.getPath()));
                downloadImage();
            }
        });
        button.setOnClickListener(v -> {
            specifyFilePathAndName();
            if(fileName!=null && filePath!=null){
                textView.setText(BodyAttr.body_attr(filePath.getPath()));
                readImage();
            }
        });
        radioButton.setOnClickListener(v -> {
            if(radioButton.isChecked()){
                internalStorage = true;
            }
        });
        radioButton2.setOnClickListener(v -> {
            if(radioButton2.isChecked()){
                internalStorage = false;
            }
        });
        radioButton3.setOnClickListener(v -> {
            if(radioButton3.isChecked()){
                persistentFiles = true;
            }
        });
        radioButton4.setOnClickListener(v -> {
            if(radioButton4.isChecked()){
                persistentFiles = false;
            }
        });
    }

    public void specifyFilePathAndName(){
        if (internalStorage!=null && persistentFiles!=null) {
            if (internalStorage && persistentFiles) {
                imageURL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.qqzhi.com%2Fuploads%2F2018-12-18%2F152156189.jpg&refer=http%3A%2F%2Fimg.qqzhi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623640158&t=e1169b3ba347c2a4b1c65dd78db29b2b";
                fileName = "2F152156189.jpg";
                filePath = getFilesDir();
            } else if (internalStorage && !persistentFiles) {
                imageURL = "https://inews.gtimg.com/newsapp_bt/0/13435315611/1000";
                fileName = "1000.jfif";
                filePath = getCacheDir();
            } else if (!internalStorage && persistentFiles) {
                imageURL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20190924%2F5acac4004e374e84a8870f30c5e59fd8.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623640630&t=ed59f186f4b8292d767614759b7e7c48";
                fileName = "doubanio1.jfif";
                filePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
            } else if (!internalStorage && !persistentFiles) {
                imageURL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp215017975.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623640630&t=7ca00f2b08bb8c6b4d71c4269b7e1290";
                fileName = "doubanio2.jfif";
                filePath = getExternalCacheDir();
            }
        }
    }
}